/**
 * 发送邮箱验证码
 * @param email 用户输入邮箱
 * @param that
 */
function ajaxGetCodeToEmail(email, that) {
    var url = "/Lvscore_User_M/register/ajaxGetCodeToEmail";
    $.post(url, {
        email: email
    }, function (data) {
        /* 成功：发送成功
         失败：该邮箱已被注册/服务器异常，请稍候重试*/
        $(that).removeAttr("disabled", "disabled");
        if (data == '1') {
            alertBox("发送成功，请注意查收");
            settime(that);
        }
        else if (data == '-1') {
            setTimeout(function(){
                $("#emailError").text("该邮箱已被注册");
                that.removeAttribute("disabled");
                that.value = "获取验证码";
                that.style.opacity = "1";
            },1000);
        }
        else {
            setTimeout(function(){
                alertBox("发送失败，服务器异常", '');
                that.removeAttribute("disabled");
                that.value = "获取验证码";
                that.style.opacity = "1";
            },1000);

        }
    })
}
/**
 * 获取当前操作用户信息
 */
function ajaxGetUser() {
    var url = "/Lvscore_User_M/register/ajaxGetUserByRegister";
    $.post(url, {}, function (data) {
        $(".tel").text(data.mobile);
        $(".userId").text(data.id);
        $(".nickname").text(data.name);
    });
}
function isRegisterEmail(email) {
    if (!isNotEmpty(email)) {
        $("#emailError").text("请输入邮箱");
        return false;
    } else if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(email)) {
        $("#emailError").text("邮箱格式不正确");
        return false;
    } else {
        $("#emailError").text("");
        return true;
    }
}
function isRegsiterCode(email, code) {
    if (isRegisterEmail(email)) {
        if (!isNotEmpty(code)) {
            $("#codeError").text("请输入验证码");
            return false;
        } else if (!/^[A-Za-z0-9]{6}$/.test(code)) {
            $("#codeError").text("验证码错误");
            return false;
        } else {
            var s = false;
            var url = "/Lvscore_User_M/register/ajaxValidateCodeForEmail";
            $.ajax({
                type: "post",
                url: url,
                data: {email: email, code: code},
                async: false,
                success: function (data) {
                    if (data == 1) {
                        $("#codeError").text("");
                        s = true;
                    }else{
                        $("#codeError").text("验证码错误");
                    }
                }
            });
            return s;
        }
    }
}
$(function () {
    $(".email").val("");
    $(".verticalCode").val("");
    getUrl("register/registerTip", "register/detailInfm");
    ajaxGetUser();
    //实时监控输入框
    inputChange("#emil","#emailError");
    inputChange(".verticalCode","#codeError");
    $(".getCode").click(function () {
        //邮箱验证
        var emails = $(".email").val();
        if (isRegisterEmail(emails)) {
            $(".getCode").attr("disabled", "disabled");
            $(".getCode").val("发送中...");
            ajaxGetCodeToEmail(emails, this);
        }
    });
    $(".email").blur(function () {
        var email = $(".email").val();
        isRegisterEmail(email);
    });
    $(".verticalCode").blur(function () {
        var email = $(".email").val();
        var code = $(".verticalCode").val();
        isRegsiterCode(email, code);
    });
    //下一步提交
    //性别
    $(".next").click(function () {
        var user = {};
        user.email = $(".email").val();
        user.code = $(".verticalCode").val();
        user.age = $('#age').val();
        user.sex = getRadioValue("sex");
        //var emailInf=$("#emailError").text();
        //$("#emailError").text(emailInf);
        if (isRegsiterCode(user.email, user.code)) {
            var cookie = new sweetCookie();
            cookie.setCookie("r_user", encodeBase64(encodeURIComponent(JSON.stringify(user)), 3), 30 * 60000);
            window.location.href = "/Lvscore_User_M/userWeb/views/register/detailInfm.html"
        }
    });
});